package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.qk.common.constant.CacheConstants;
import com.qk.dto.business.BusinessCountDo;
import com.qk.dto.clue.ClueCountDo;
import com.qk.management.aop.anno.ReportRedisAnno;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.service.ReportOverviewService;
import com.qk.vo.portal.PortalVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @文件名: ReportOverviewServiceImpl
 * @作者: DX
 * @创建日期: 2025/10/15
 * @描述:
 */
@Slf4j
@Service
@SuppressWarnings("all")
public class ReportOverviewServiceImpl implements ReportOverviewService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String redisKey = CacheConstants.CACHE_PORTAL_KEY_PREFIX + "reportOverview";

    @Override
    public PortalVo reportOverviewWitchCache() {

        String jsonStr = null;

        try {
            jsonStr =  redisTemplate.opsForValue().get(redisKey);

            if (ObjectUtil.isNotNull(jsonStr)) {
                PortalVo vo = JSONUtil.toBean(jsonStr, PortalVo.class);
                return vo;
            }
        } catch (Exception e) {
            log.error("redis缓存获取数据异常{}" , e );
        }

        ClueCountDo clueCountDo = clueMapper.selectCountClue();
        BusinessCountDo businessCountDo = businessMapper.selectCountBusiness();
        PortalVo vo = BeanUtil.copyProperties(clueCountDo, PortalVo.class);
        BeanUtil.copyProperties(businessCountDo, vo);

        try {
            String jsonStr1 = JSONUtil.toJsonStr(vo);
            redisTemplate.opsForValue().set(redisKey, jsonStr1);
        } catch (Exception e) {
            log.error("redis缓存保存数据异常{}" , e );
        }
        return vo;
    }

    @Override
    public void refreshPortalData() {

        try {
            PortalVo portalData = reportOverview();
            String jsonStr = JSONUtil.toJsonStr(portalData);
            redisTemplate.opsForValue().set(redisKey, jsonStr);
            log.info("数据缓存完成");
        } catch (Exception e) {
            log.error("数据缓存强制刷新异常", e);
        }
    }
    
    @ReportRedisAnno(
            keyPrefix = CacheConstants.CACHE_PORTAL_KEY_PREFIX,
            exPireTime = 24*60l,
            expireTimeUnit = TimeUnit.MINUTES
    )
    @Override
    public PortalVo reportOverview() {
        ClueCountDo clueCountDo = clueMapper.selectCountClue();
        BusinessCountDo businessCountDo = businessMapper.selectCountBusiness();
        PortalVo vo = BeanUtil.copyProperties(clueCountDo, PortalVo.class);
        BeanUtil.copyProperties(businessCountDo, vo);
        return vo;
    }
}